import Shop from '../../model/shop'
import DlyAdminUser from '../../model/dlyadminuser'
import Orders from '../../model/orders'
import ApiError from '../../error/ApiError'
import ApiErrorNames from '../../error/ApiErrorNames'
import AiPayOrder from '../../model/AiPayOrder'
import AiOrder from '../../model/AiOrder'

class OrdersController {
	static async serviceMarketlist(ctx) {
		const jwtuserinfo = ctx.request.header.jwtuserinfo
		console.log('jwtuserinfo', jwtuserinfo)
		const {
			page = 1,
			pageSize = 20,
			// isBuy = '',
			shop_id = '',
			// shop_name = '',
			shop_phone = '',
			user_phone = '',
			isProxy = '',
			proxyPhone = '',
			order_id = '',
			isRefund = '',
			pay_type = '',
		} = ctx.query
		const param = {
			shop_id,
			order_id,
			isRefund,
			pay_type,
		}
		if (
			jwtuserinfo.roleName === 'adminSp' ||
			jwtuserinfo.roleName === 'admin'
		) {
			param.proxyId = ''
		}
		if (jwtuserinfo.roleName === 'proxy1') {
			const pritids = await DlyAdminUser.find({
				parentId: jwtuserinfo._id,
			})
			if (pritids.length > 0) {
				const litsadrtid = [
					jwtuserinfo._id,
					...pritids.map((item) => item._id),
				]
				param.proxyId = { $in: litsadrtid }
			} else {
				param.proxyId = jwtuserinfo._id
			}
		}
		if (jwtuserinfo.roleName === 'proxy2') {
			param.proxyId = jwtuserinfo._id
		}
		if (proxyPhone != '') {
			const getProxyData = await DlyAdminUser.findOne({
				phone: proxyPhone,
			})
			if (!getProxyData) {
				ctx.body = { list: [], total: 0 }
				return
			}
			param.proxyId = getProxyData._id
		}
		for (let key in param) {
			if (!param[key]) delete param[key]
		}
		const list = await Orders.find(param)
			.populate('proxyId')
			.populate('proxyLinkId')
			.limit(pageSize * 1)
			.skip((page - 1) * pageSize)
			.sort({ createTime: -1 })
		console.log('ctx', ctx.request.header.jwtuserinfo)
		console.log('quers', ctx.query)
		const total = await Orders.countDocuments(param)
		ctx.body = { list, total }
	}
	static async alipayOrders(ctx) {
		// ctx.body = '支付宝订单'
		const jwtuserinfo = ctx.request.header.jwtuserinfo
		if (
			jwtuserinfo.roleName !== 'adminSp' &&
			jwtuserinfo.roleName !== 'admin'
		) {
			throw new ApiError(ApiErrorNames.NO_PERMISSION)
		}
		const {
			page = 1,
			pageSize = 20,
			subject = '',
			status = '',
			func_name = '',
		} = ctx.query
		const param = {
			subject,
			status,
			func_name,
		}
		if (param.subject) {
			param.subject = { $regex: param.subject, $options: 'i' }
		}
		for (let key in param) {
			if (!param[key]) delete param[key]
		}
		if (param.func_name && param.func_name == 'AI') {
			// 筛选null 或者 AI
			param.func_name = { $in: [null, 'AI'] }
		}
		const list = await AiPayOrder.find(param)
			.populate('order_user_id')
			.limit(pageSize * 1)
			.skip((page - 1) * pageSize)
			.sort({ createTime: -1 })
		const total = await AiPayOrder.countDocuments(param)
		ctx.body = { list, total }
	}
	static async orders(ctx) {
		console.log('orders')
		const {
			page = 1,
			pageSize = 20,
			start_time = '',
			end_time = '',
		} = ctx.query
		const jwtuserinfo = ctx.request.header.jwtuserinfo
		if (
			jwtuserinfo.roleName !== 'adminSp' &&
			jwtuserinfo.roleName !== 'admin'
		) {
			throw new ApiError(ApiErrorNames.NO_PERMISSION)
		}
		const param = {}
		if (start_time && end_time) {
			param.createTime = {
				$gte: new Date(start_time),
				$lte: new Date(end_time),
			}
		}
		const list = await AiOrder.find(param)
			.populate('ai_user_id')
			.populate('shop_id')
			.limit(pageSize * 1)
			.skip((page - 1) * pageSize)
			.sort({ createTime: -1 })
		const total = await AiOrder.countDocuments(param)
		ctx.body = {
			list,
			total,
		}
	}
}

export default OrdersController
